package com.example.tool.controller.worktool;

import com.alibaba.fastjson.JSONObject;
import com.example.tool.bean.business.WarehouseInterceptionConfig;
import com.example.tool.bean.form.StatisticStockSpecInForm;
import com.example.tool.bean.wcaiexcel.RequestData;
import com.example.tool.service.claimsinfo.ClaimsInfoService;
import com.example.tool.service.dingtalk.DingTalkBusinessService;
import com.example.tool.service.task.TaskService;
import com.example.tool.util.DDUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/workTool/api")
public class WorkToolController {
    @Autowired
    private ClaimsInfoService claimsInfoService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private DingTalkBusinessService dingTalkBusinessService;

    public static HashMap<String, WarehouseInterceptionConfig> warehouseConf = new HashMap<>();
    public static List<String> headerValues = Arrays.asList("快递单号", "快递公司", "退款单号", "原始单号", "仓库", "店铺", "录入日期");

    static {
        warehouseConf.put("江高常温", new WarehouseInterceptionConfig("gvNG4YZ7JnbMzLR5UydL14rDJ2LD0oRE", "", "", null, null));
        warehouseConf.put("长沙常温", new WarehouseInterceptionConfig("o14dA3GK8gzNMZ2YF93RXO0BJ9ekBD76", "", "", null, null));
        warehouseConf.put("义乌常温", new WarehouseInterceptionConfig("o14dA3GK8gzNMZ2YF93RayQvJ9ekBD76", "", "", null, null));
        warehouseConf.put("龙港常温", new WarehouseInterceptionConfig("dpYLaezmVNgO3NE5iZnEoKrzJrMqPxX6", "", "", null, null));
        warehouseConf.put("江西常温", new WarehouseInterceptionConfig("YndMj49yWjmvkeaxi0NvjYg383pmz5aA", "", "", null, null));
    }

    @PostMapping("/thirdQa")
    public void thirdQa(@RequestBody RequestData data) {
        claimsInfoService.storageClaimsInfo(data);
    }

    @PostMapping("/data")
    public void data(@RequestBody JSONObject data) {
        log.info("退件拆包接口接收:{}", data.toJSONString());
        // 模拟处理时间
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if (data.isEmpty()) {
            return;
        }
        LocalDateTime today = LocalDateTime.now();
        DateTimeFormatter FORMATTER_YYYY_MM = DateTimeFormatter.ofPattern("yyyy-MM");
        DateTimeFormatter FORMATTER_YYYY_MM_DD_HH_mm_ss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String todayStr = today.format(FORMATTER_YYYY_MM_DD_HH_mm_ss);
        String sheetName = today.format(FORMATTER_YYYY_MM);
        String groupName = data.getString("groupName");
        if (!groupName.contains("信息同步测试群")) {
            return;
        }
        String spoken = data.getString("spoken");
        boolean contains = spoken.contains("以下退货单仓库已签收，请及时拆包！");
        if (!contains) {
            return;
        }
        System.out.println(data.toString());
        List<String> split = Arrays.asList(spoken.split("\\n\\n"));
        //快递单号
        String trackNumber = split.get(1).split("：")[1];
        //快递公司
        String courierCompany = split.get(2).split("：")[1];
        //退款单号
        String refundNumber = split.get(3).split("：")[1];
        //原始单号
        String originalNumber = split.get(4).split("：")[1];
        //仓库
        String warehouse = split.get(5).split("：")[1];
        //店铺
        String shop = split.get(6).split("：")[1];
        WarehouseInterceptionConfig warehouseInterceptionConfig = warehouseConf.get(warehouse);
        String workbookId = warehouseInterceptionConfig.getWorkbookId();
        List<List<String>> values = Collections.singletonList(Arrays.asList(trackNumber, courierCompany, refundNumber, originalNumber, warehouse, shop, todayStr));
        log.info("接口AI表格录入:{}", JSONObject.toJSONString(values));
        DDUtil.importAiExcel(workbookId, values, sheetName, headerValues, 1);
    }

    @GetMapping("/schedule/stock/statistic")
    @Async
    public void statisticStockSchedule() {
        log.info("---------ERP库存统计手动任务开始----------");
        taskService.scheduledImportClaims();
        log.info("---------ERP库存统计手动任务结束----------");
    }

    @PostMapping("/stockIn/statistic")
    @Async
    public void statisticStockSpecIn(@RequestBody StatisticStockSpecInForm specInForm) {
        log.info("---------商品入库统计记录开始----------");
        taskService.statisticStockSpecIn(specInForm);
        log.info("---------商品入库统计记录结束----------");
    }

    /**
     * 钉钉用户新增录入企微
     */
    @GetMapping("/dingTalk/weChat/addUser")
    public void addDingTalkUserSendToWeChat(@RequestParam("userId") String userId) {
        log.info("---------钉钉用户{}入职同步企微开始----------", userId);
        dingTalkBusinessService.addDingTalkUserSendToWeChat(userId);
        log.info("---------钉钉用户入职同步企微结束----------");
    }

    /**
     * 钉钉用户新增录入企微
     */
    @GetMapping("/dingTalk/weChat/deleteUser")
    public void deleteDingTalkUserSendToWeChat(@RequestParam("userId") String userId) {
        log.info("---------钉钉用户{}离职同步企微开始----------", userId);
        dingTalkBusinessService.deleteDingTalkUserSendToWeChat(userId);
        log.info("---------钉钉用户离职同步企微结束----------");
    }
}

